#!bin/bash

function reset_env() {
	systemctl stop nginx;
	systemctl stop httpd;
	pkill -9 nginx;
	pkill -9 httpd;
	rm -f /var/log/nginx/*
	rm -f /var/log/httpd/*
	sleep 3
	systemctl start httpd;
	systemctl start nginx;	
}


function core_binding() {
	cpus=$1;
	pstree -pn 1 |grep -o "([[:digit:]]*)" |grep -o "[[:digit:]]*" > .tmp
	cat .tmp | while read line
	do
        	taskset -pc $cpus $line
	done
}

function senario_testing() {
	for i in {1..3};do
		sleep 70
		#start_time=$(date +"%Y-%m-%d %H:%M:%S");
		start_time=$(date +"%m.%d %H:%M:%S");
        	echo "stime:" $start_time
        	ab -kc $1 -n 2000000 http://172.168.122.76/var/www/html > /dev/null &
        	ab -kc $1 -n 2000000 http://172.168.122.76/var/www/html > /dev/null &
        	ab -kc $1 -n 2000000 http://172.168.122.76/var/www/html > /dev/null &
        	ab -kc $1 -n 2000000 http://172.168.122.76/var/www/html | grep "Time taken for tests:"
        	#end_time=$(date +"%Y-%m-%d %H:%M:%S");
        	end_time=$(date +"%m.%d %H:%M:%S");
		echo "etime:" $end_time
		sh ./powertool.sh avg $start_time $end_time | grep "Sys_Average_Watt"
		
	done
}


function_watt_tasks() {
	pstree -pn 1 |grep -o "([[:digit:]]*)" |grep -o "[[:digit:]]*" > .tmp
	echo 60 > /proc/sys/kernel/sched_util_low_pct

	mkdir -p /sys/fs/cgroup/cpu/test_watt/
	cat .tmp | while read line
	do
		echo $line > /sys/fs/cgroup/cpu/test_watt/tasks
	done
	echo 1 > /sys/fs/cgroup/cpu/test_watt/cpu.dynamic_affinity_mode
}

function_watt_tasks_release() {
	echo 0 > /sys/fs/cgroup/cpu/test_watt/cpu.dynamic_affinity_mode
	cgdelete cpu:/test_watt/
}

load_level=("3" "16" "30");
cpu_usage=("10" "20" "27");

function feature_testing() {

	sh ./powertool.sh collect 1 &
	for i in {0..2}
	do
		echo "load level:" ${cpu_usage[$i]} "start testing=================================================";
		#reset_env > /dev/null
		#echo "Service reset----------------------"
		#sleep 5;
		senario_testing ${load_level[$i]}
	done
	kill -9 `ps -ef | grep powertool.sh | grep -v grep | awk '{print $2}'`
	rm -f ./localpwrinfo.txt
}

core_binding 0-95 > /dev/null
reset_env > /dev/null
sleep 5

echo "Performace mode(Baseline) testing===========================================================================";
function_watt_tasks_release > /dev/null
echo 0 > /sys/fs/cgroup/cpu/user.slice/cpu.dynamic_affinity_mode
cpupower  frequency-set -g performance > /dev/null
feature_testing;

echo "Low power mode testing======================================================================================";
function_watt_tasks
cpupower  frequency-set -g conservative > /dev/null
feature_testing;
function_watt_tasks_release > /dev/null

echo "Low power + Compile opt testing==============================================================================";
systemctl stop nginx;
pkill nginx
/root/rpmbuild/BUILD/nginx-1.21.5/objs/nginx.bolt -c /etc/nginx/nginx.conf
function_watt_tasks
feature_testing;
function_watt_tasks_release > /dev/null


centos             openeuler 
                   conservative      performance
                                   
performance        4 10%            4 10% 
                                    6 15%
                                    11 20%
3  10%             8 15%
6   15%            16 20% 
16 20%           



icelake
centos

15 10% 

虚拟机：
5  10% 
10 20%

18 30%


100  10%  
80   8% 
 
30   3%， 
10   1%

50 5%


performance  


nginx 20% 
mysql 15% 